﻿using NuMvvm;
using NuMvvm.Commands;
using NuMvvm.Services;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using Todo.Models;
using Todo.Services;
using Todo.Views;

namespace Todo.ViewModels
{
    public class ItemViewModel : ViewModelBase
    {
        private string _title;
        private bool _isDone;
        private AsyncDelegateCommand _okCommand;

        public ItemViewModel(
            IDataService dataService, 
            IDialogService dialogService, 
            INavigationService navigationService)
        {
            DataService = dataService;
            DialogService = dialogService;
            NavigationService = navigationService;
        }

        public int Id { get; private set; }

        public string Title
        {
            get
            {
                return _title;
            }

            set
            {
                if(SetProperty(ref _title, value))
                {
                    OkCommand.RaiseCanExecuteChanged();
                }
            }
        }

        public bool IsDone
        {
            get
            {
                return _isDone;
            }

            set
            {
                SetProperty(ref _isDone, value);
            }
        }

        public async Task Initialize()
        {

        }

        public async Task Initialize(int id)
        {
            var item = await DataService.GetItemAsync(id);

            Id = item.Id;
            Title = item.Title;
            IsDone = item.IsDone;
        }

        public ICommand OkCommand
        {
            get
            {
                return _okCommand ?? (_okCommand = DelegateCommand.Create(async (cancellationToken) =>
                {
                    try
                    {
                        TodoItem item = null;

                        if (Id == 0)
                        {
                            item = new TodoItem();
                        }
                        else
                        {
                            item = await DataService.GetItemAsync(Id);
                        }

                        item.Id = Id;
                        item.Title = Title;
                        item.IsDone = IsDone;

                        if (Id == 0)
                        {
                            await DataService.AddItemAsync(item);
                        }
                        else
                        {
                            await DataService.UpdateItemAsync(item);
                        }

                        NavigationService.Navigate(ViewKeys.Main);
                    }
                    catch(Exception e)
                    {
                        await DialogService.ShowErrorAsync("Failed to submit item", "Failure");
                    }
                }, 
                async (cancellationToken) => !string.IsNullOrWhiteSpace(Title)));
            }
        }

        public IDataService DataService { get; private set; }

        public IDialogService DialogService { get; private set; }

        public INavigationService NavigationService { get; private set; }
    }
}
